<?php

namespace common\models\cms;

use Yii;
use common\helpers\Url;
use common\models\uvs\UvsScene;
use common\models\uvs\UvsLevel;
use common\models\uvs\UvsUserAccount;
use common\models\game\Game;

/**
 * This is the model class for table "{{%cms_special}}".
 *
 * @property int $id
 * @property string $title 名称
 * @property string $thumb 缩略图
 * @property string|null $description 描述
 * @property string|null $author 作者
 * @property string|null $template 模板
 * @property int $recommend_at 推荐时间
 * @property int $listorder 显示排序
 * @property int $publish_at 发表时间
 * @property string|null $keywords 关键词
 * @property int $map_id 围栏ID
 * @property string|null $map_data 围栏参数
 * @property int $status 状态
 * @property int $created_at 添加时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class CmsSpecial extends \common\db\ActiveRecord {

    use \common\traits\WeblinkTrait;

    /**
     * const status 状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%cms_special}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['title'], 'required'],
            [['recommend_at', 'listorder', 'publish_at', 'map_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['title'], 'string', 'max' => 80],
            [['thumb', 'description', 'keywords', 'map_data'], 'string', 'max' => 255],
            [['author'], 'string', 'max' => 50],
            [['template'], 'string', 'max' => 20],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'title' => '名称',
            'thumb' => '缩略图',
            'description' => '描述',
            'author' => '作者',
            'template' => '自定义模板',
            'recommend_at' => '推荐时间',
            'listorder' => '显示排序',
            'publish_at' => '发表时间',
            'keywords' => '关键词',
            'map_id' => '围栏ID',
            'map_data' => '围栏参数',
            'status' => '状态',
            'created_at' => '添加时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     * 关联专题扩展信息
     * @return \common\db\ActiveQuery the relational query object.
     */
    public function getData() {
        return $this->hasOne(CmsSpecialData::class, ['special_id' => 'id']);
    }

    /**
     * 关联专题频道
     * @return \common\db\ActiveQuery the relational query object.
     */
    public function getChannels() {
        return $this->hasMany(CmsSpecialChannel::class, ['special_id' => 'id']);
    }

    /**
     * 关联专题导航
     * @return \common\db\ActiveQuery the relational query object.
     */
    public function getNavs() {
        return $this->hasMany(CmsSpecialNav::class, ['special_id' => 'id']);
    }

    /**
     * 关联专题文章
     * @return \common\db\ActiveQuery the relational query object.
     */
    public function getSpecialArticles() {
        return $this->hasMany(CmsSpecialArticle::class, ['special_id' => 'id']);
    }

    /**
     * 关联专题答题闯关
     * @return \common\db\ActiveQuery the relational query object.
     */
    public function getGame() {
        return $this->hasOne(Game::class, ['usage_id' => 'id'])->active(Game::STATUS_ACTIVE)->andWhere([
            'usage' => Game::USAGE_SPECIAL
        ]);
    }

    /**
     * 关联专题用户成长场景
     * @return \common\db\ActiveQuery the relational query object.
     */
    public function getUvsScene() {
        return $this->hasOne(UvsScene::class, ['usage_id' => 'id'])->active(UvsScene::STATUS_ACTIVE)->andWhere([
            'usage' => UvsScene::USAGE_SPECIAL
        ]);
    }

    /**
     * 关联专题用户成长等级
     * @return \common\db\ActiveQuery the relational query object.
     */
    public function getUvsLevels() {
        return $this->hasMany(UvsLevel::class, ['scene_id' => 'id'])->via('uvsScene')->active(UvsLevel::STATUS_ACTIVE)->orderBy([
            'no' => SORT_ASC
        ]);
    }

    /**
     * 关联专题用户成长场景账户
     * @return \common\db\ActiveQuery the relational query object.
     */
    public function getUvsUserAccount() {
        return $this->hasOne(UvsUserAccount::class, ['scene_id' => 'id'])->via('uvsScene')->active(UvsUserAccount::STATUS_ACTIVE)->andWhere([
            'user_id' => Yii::$app->user->id
        ]);
    }

    /**
     * 根据日期获取专题日历型文章
     * @return \common\db\ActiveQuery the relational query object.
     */
    public function getCalendarTodayArticle($date = null) {
        if (!$date) {
            $date = date('m-d');
        }
        $query = $this->getSpecialArticles();
        $query->innerJoinWith('article article');
        return $query->andWhere([
            'like', 'article.date', '%-' . $date, false
        ])->debug()->one();
    }

    /**
     * 获取专题封面图地址
     * @return string
     */
    public function getCover() {
        return Url::attach($this->thumb);
    }

    /**
     * 获取专题Banner图地址
     * @return string
     */
    public function getBanner() {
        if (!$this->data) {
            return '';
        }
        return Url::attach($this->data->banner);
    }

    /**
     * 获取专题背景图地址
     * @return string
     */
    public function getBg() {
        if (!$this->data) {
            return '';
        }
        return Url::attach($this->data->bg);
    }

    /**
     * 获取专题跳转地址标题
     * @return string
     */
    public function getMapTitle() {
        return $this->title;
    }

}
